
/**
 * Remove an element from the specified array, and return a copy of the generated array.
 * @param array the array to operate
 * @param index index of the element to delete
 * @returns the array without the specified element, the function does not modify the source list
 */
function removeElementFromArray<T>(array: T[], index: number): T[] {
  if (array.length === 0 || index < 0 || index >= array.length) {
    throw new RangeError();
  }
  return [
    ...array.slice(0, index),
    ...array.slice(index + 1)
  ];
}

/**
 * Replace the specified element of the array, and return a copy of the generated array.
 * @param array the array to operate
 * @param index index of the element to replace
 * @param target the new element to add to the cloned array
 * @returns the array which the target element is replaced, the function does not modify the source list
 */
function replaceElementFromArray<T>(array: T[], index: number, target: T): T[] {
  if (array.length === 0 || index < 0 || index >= array.length) {
    throw new RangeError();
  }
  return [
    ...array.slice(0, index),
    target,
    ...array.slice(index + 1)
  ];
}

/**
 * Clone the given array.
 * @param array the array to clone
 * @returns a clone of the given array, with a different reference
 */
function cloneArray<T>(array: T[]): T[] {
  return [
    ...array
  ];
}


export { removeElementFromArray, replaceElementFromArray, cloneArray };